const signInBtn = document.getElementById("signIn");
const signUpBtn = document.getElementById("signUp");
const fistForm = document.getElementById("form1");
const secondForm = document.getElementById("form2");
const container = document.querySelector(".container");

if (signInBtn) {
    signInBtn.addEventListener("click", () => {
        container.classList.remove("right-panel-active");
    });
}

if (signUpBtn) {
    signUpBtn.addEventListener("click", () => {
        container.classList.add("right-panel-active");
    });
}

if (fistForm) {
    fistForm.addEventListener("submit", (e) => {
        e.preventDefault();  // 阻止表单默认提交
        signup();
    });
}

if (secondForm) {
    secondForm.addEventListener("submit", (e) => {
        e.preventDefault();  // 阻止表单默认提交
        login();
    });
}

function signup() {
    const formData = new FormData(fistForm); // 使用正确的表单元素创建FormData对象
    axios.post('/accounts/register/', formData)
    .then(function (response) {
        console.log(response.data);
        // alert("注册成功！");
        container.classList.remove("right-panel-active");
        fistForm.reset();
    })
    .catch(function (error) {
        console.log(error.response.data);
        alert("注册失败：" + error.response.data.error);
    });
}

function login() {
    const formData = new FormData(secondForm); // 使用正确的表单元素创建FormData对象
    axios.post('/accounts/login/', formData)
    .then(function (response) {
        console.log(response.data);
        // alert("登录成功！");
        window.location.href = "/music/genre/";
    })
    .catch(function (error) {
        console.log(error.response.data);
        alert("登录失败：" + error.response.data.error);
    });
}
